<?php


namespace app\common\model;


use app\common\controller\BaseController;

class CoreSetting extends Model
{
    static $debug = false;
    public $columns = [
        [  "type"=> self::TYPE_VARCHAR,  "column"=>"key",  "memo"=>"key", "length"=> 64 ],
        [  "type"=> self::TYPE_TEXT,  "column"=>"value",  "memo"=>"value", "length"=> 511 ],
    ];
    public static function debug($is_open = false){
        self::$debug = $is_open;
        return new self();
    }
    public static function conf($key,$value=null){
        $index = static::class."_".$key;
        $data = static::where(["key"=>$key])->find();
        if(empty($value)){
            //缓存
            if(self::$debug){
                $value = BaseController::$cache->get($index);
                if($value){
                    return $value;
                }
            }
            return isset($data["value"])?$data["value"]:false;
        }else{
            if(is_array($value)){
                $value = json_encode($value);
            }
            if(!$data){
                $data  = new static();
            }
            $data->value = $value;
            $data->save();
            //缓存
            if(self::$debug){
                $value = BaseController::$cache->set($index,$value);
            }
        }
    }
}